package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.ContractProduceEnum;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.NumberTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.dictionary.AllOutBoundEnum;
import com.xbongbong.saas.enums.dictionary.ArchivedEnum;
import com.xbongbong.saas.enums.dictionary.ContractTypeEnum;
import com.xbongbong.saas.enums.dictionary.ExpressStatusEnum;
import com.xbongbong.saas.enums.dictionary.HasRedEnum;
import com.xbongbong.saas.enums.dictionary.IfStatisticEnum;
import com.xbongbong.saas.enums.dictionary.IsRedEnum;
import com.xbongbong.saas.enums.dictionary.PayMethodEnum;
import com.xbongbong.saas.enums.dictionary.ShipStatusEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/18 21:01
 * @since v1.0
 */
@Getter
public enum TempContractEnum {
    //模板设计展示字段
    BASE_INFO(ContractEnum.BASE_INFO),
    CONTRACT_NO(ContractEnum.CONTRACT_NO),
    NAME(ContractEnum.NAME),
    LINK_CUSTOMER(ContractEnum.LINK_CUSTOMER),
    LINK_CONTACT(ContractEnum.LINK_CONTACT),
    LINK_OPPORTUNITY(ContractEnum.LINK_OPPORTUNITY),
    QUOTATION_ID(ContractEnum.QUOTATION_ID),
    SIGN_PERSON(ContractEnum.SIGN_PERSON),
    SIGN_TIME(ContractEnum.SIGN_TIME),
    DEADLINE(ContractEnum.DEADLINE),
    STATUS(ContractEnum.STATUS),
    OWNER_ID(ContractEnum.OWNER_ID),
    MINOR_USER(ContractEnum.MINOR_USER),
    SALE_INFO(ContractEnum.SALE_INFO),
    PRODUCT(ContractEnum.PRODUCT),
    PRODUCT_TOTAL(ContractEnum.PRODUCT_TOTAL),
    DISCOUNT(ContractEnum.DISCOUNT),
    OTHER_EXPENSE(ContractEnum.OTHER_EXPENSE),
    DISCOUNT_AMOUNT(ContractEnum.DISCOUNT_AMOUNT),
    AMOUNT(ContractEnum.AMOUNT),
    CONTRACT_COST(ContractEnum.CONTRACT_COST),
    GROSS_PROFIT(ContractEnum.GROSS_PROFIT),
    GROSS_PROFIT_RATE(ContractEnum.GROSS_PROFIT_RATE),
    CASH_PROFIT(ContractEnum.CASH_PROFIT),
    CASH_PROFIT_RATE(ContractEnum.CASH_PROFIT_RATE),
    ASSOCIATED_INFO(ContractEnum.ASSOCIATED_INFO),
    ADD_WRITE_OFF_PREPAYMENT(ContractEnum.ADD_WRITE_OFF_PREPAYMENT),
    ADD_PAYMENT_SHEET(ContractEnum.ADD_PAYMENT_SHEET),
    ADD_PAYMENT(ContractEnum.ADD_PAYMENT),
    EXTEND_INFO(ContractEnum.EXTEND_INFO),
    LINK_PARTNER_CUSTOMER(ContractEnum.LINK_PARTNER_CUSTOMER),
    TYPE(ContractEnum.TYPE),
    PAY_METHOD(ContractEnum.PAY_METHOD),
    EXPRESS_STATUS(ContractEnum.EXPRESS_STATUS),
    SHIP_STATUS(ContractEnum.SHIP_STATUS),
    EXPRESS_NAME(ContractEnum.EXPRESS_NAME),
    EXPRESS_NO(ContractEnum.EXPRESS_NO),
    SYSTEM_INFO(ContractEnum.SYSTEM_INFO),
    CREATOR_ID(ContractEnum.CREATOR_ID),
    ADD_TIME(ContractEnum.ADD_TIME),
    UPDATE_TIME(ContractEnum.UPDATE_TIME),
    FINISH_AMOUNT(ContractEnum.FINISH_AMOUNT),
    UN_FINISH_AMOUNT(ContractEnum.UN_FINISH_AMOUNT),
    BAD_AMOUNT(ContractEnum.BAD_AMOUNT),
    PAY_PERCENT(ContractEnum.PAY_PERCENT),
    INVOICE_AMOUNT(ContractEnum.INVOICE_AMOUNT),
    UN_INVOICE_AMOUNT(ContractEnum.UN_INVOICE_AMOUNT),
    IF_STATISTIC(ContractEnum.IF_STATISTIC),
    CURRENCY(ContractEnum.CURRENCY),
    EXCHANGE_RATE(ContractEnum.EXCHANGE_RATE),
    INSTOCK_ID(ContractEnum.INSTOCK_ID),
    OUTSTOCK_ID(ContractEnum.OUTSTOCK_ID),
    PURCHASE_ID(ContractEnum.PURCHASE_ID),
    ALL_OUT_BOUND(ContractEnum.ALL_OUT_BOUND),
    IS_RED(ContractEnum.IS_RED),
    HAS_RED(ContractEnum.HAS_RED),
    ESTIMATE_COST(ContractEnum.ESTIMATE_COST),
    REAL_COST(ContractEnum.REAL_COST),
    SHOW_PRODUCE(ContractEnum.SHOW_PRODUCE),
    LINK_CUSTOMER_HIDE(ContractEnum.LINK_CUSTOMER_HIDE),
    LINK_CONTACT_HIDE(ContractEnum.LINK_CONTACT_HIDE),
    LINK_OPPORTUNITY_HIDE(ContractEnum.LINK_OPPORTUNITY_HIDE),
    LINK_PARTNER_CUSTOMER_HIDE(ContractEnum.LINK_PARTNER_CUSTOMER_HIDE),
    QUOTATION_NAME(ContractEnum.QUOTATION_NAME),
//    UNIT(ContractEnum.UNIT),
    ARCHIVED(ContractEnum.ARCHIVED),
    SIGN_NAME(ContractEnum.SIGN_NAME),
    RECEIVABLE_PERIOD(ContractEnum.RECEIVABLE_PERIOD),
    // 已出库其他费用
    OUTSTOCK_OTHER_EXPENSE(ContractEnum.OUTSTOCK_OTHER_EXPENSE),
    // 已出库优惠金额
    OUTSTOCK_DISCOUNT_AMOUNT(ContractEnum.OUTSTOCK_DISCOUNT_AMOUNT),

    ;
    /**
     * 合同枚举
     */
    private ContractEnum anEnum;

    TempContractEnum(ContractEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempContractEnum> getAllEnum() {
        List<TempContractEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static void initContractNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.CONTRACT.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.CONTRACT.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(CONTRACT_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setSerialNumber(InitExplainsHelper.setSerialNumber(SerialNumberEnum.CONTRACT_NO));
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initName(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.CONTRACT.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.CONTRACT.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(NAME.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
    }

    public static void initLinkCustomer(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(LINK_CUSTOMER_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initQuotationId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.QUOTATION.getCode());
        fieldAttrEntity.setLinkedText(QUOTATION_NAME.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        // 合同新建编辑时，报价单的父业务为机会和客户，机会的优先级大于客户
        JSONArray array = new JSONArray();
        JSONObject relyCustomer = new JSONObject();
        relyCustomer.put(StringConstant.ATTR, ContractEnum.LINK_CUSTOMER.getAttr());
        relyCustomer.put(StringConstant.BUSINESS_TYPE, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        relyCustomer.put("priority", 1);
        JSONObject relyOpp = new JSONObject();
        relyOpp.put(StringConstant.ATTR, ContractEnum.LINK_OPPORTUNITY.getAttr());
        relyOpp.put(StringConstant.BUSINESS_TYPE, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
        relyOpp.put("priority", 2);
        array.add(relyCustomer);
        array.add(relyOpp);
        saasParticularAttributePoJo.setRelyArray(array);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(1);
    }

    /**
     * 整单折扣默认为百分比，所以默认值为1（新建编辑时候就会默认展示为100%）
     * @param fieldAttrEntity
     */
    public static void initDiscount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.NO.getType());
        fieldAttrEntity.setIntegerOnly(BasicConstant.ZERO);
        fieldAttrEntity.setNumberType(NumberTypeEnum.RATE.getCode());
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMax(100D);
        numericalLimits.setMin(1D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        defaultAttrPoJo.setDefaultValue(BasicConstant.ONE);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }

    /**
     * 初始化添加回款
     *
     * @param fieldAttrEntity 字段
     * @author feng.zheng
     * @return void
     * @date 2019/2/20 19:06
     */
    public static void initAddPayment(FieldAttrEntity fieldAttrEntity) {
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setLinkBusinessType(XbbRefTypeEnum.PAYMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumericalLimits(new NumericalLimitsPoJo(0D,50D));
    }

    public static void initAddPaymentSheet(FieldAttrEntity fieldAttrEntity) {
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setLinkBusinessType(XbbRefTypeEnum.PAYMENT_SHEET.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumericalLimits(new NumericalLimitsPoJo(0D,50D));
        fieldAttrEntity.setIsOpen(0);
    }

    public static void initAddWriteOffPrepayment(FieldAttrEntity fieldAttrEntity) {
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setLinkBusinessType(XbbRefTypeEnum.WRITE_OFF_PREPAYMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        fieldAttrEntity.setNumericalLimits(new NumericalLimitsPoJo(0D,50D));
        fieldAttrEntity.setIsOpen(0);
    }

    public static void initLinkContact(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CONTACT.getCode());
        fieldAttrEntity.setLinkedText(LINK_CONTACT_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 无签订人特殊导入
     *
     * @param fieldAttrEntity
     */
    public static void initSignPerson(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedText(SIGN_NAME.getAnEnum().getAttr());
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initLinkOpportunity(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.SALES_OPPORTUNITY.getCode());
        fieldAttrEntity.setLinkedText(LINK_OPPORTUNITY_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    public static void initLinkPartnerCustomer(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(LINK_PARTNER_CUSTOMER_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsOpen(BasicConstant.ZERO);
    }

    public static void initSignTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initDeadLine(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType("yyyy-MM-dd");
    }


    public static void initUnit(FieldAttrEntity fieldAttrEntity) {
        String str = "元,美元,欧元,日元,万,港币,万元";
        List<ItemPoJo> items = InitExplainsHelper.setCombItem(str);
        items.get(0).setChecked(true);
        fieldAttrEntity.setItems(items);
    }

    public static void initStatus(FieldAttrEntity fieldAttrEntity) {
//        String str = "签约,执行中,完毕,意外中止";
        fieldAttrEntity.setItems(new ArrayList<>());
        fieldAttrEntity.setComboType(ComboTypeEnum.CUSTOM.getType());
        fieldAttrEntity.setDictionaryFlag(0);
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initType(FieldAttrEntity fieldAttrEntity) {
//        String str = "直销合同,代理合同,服务合同,快销合同";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ContractTypeEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }

    public static void initPayMethod(FieldAttrEntity fieldAttrEntity) {
//        String str = "网银转账,现金,支票,电汇,承兑汇票";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(PayMethodEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }

    public static void initExpressStatus(FieldAttrEntity fieldAttrEntity) {
//        String str = "缺货,待发货,已发货";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ExpressStatusEnum.class, fieldAttrEntity.getFieldType()));
//        fieldAttrEntity.setDictionaryFlag(1);
    }
    public static void initTotalMoney(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("默认值=产品合计*整单折扣+其他费用-优惠金额");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUBTRACT(SUM({self." + ContractEnum.PRODUCT_TOTAL.getAttr() + "}*IF(OR(ISEMPTY({self." + ContractEnum.DISCOUNT.getAttr() +"}),{self." + ContractEnum.DISCOUNT.getAttr() + "}==0),1,{self." + ContractEnum.DISCOUNT.getAttr() + "}),{self." + ContractEnum.OTHER_EXPENSE.getAttr() + "}),{self." + ContractEnum.DISCOUNT_AMOUNT.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + ContractEnum.PRODUCT_TOTAL.getAttr(), ContractEnum.PRODUCT_TOTAL.getAttrName());
        labelMap.put("self." + ContractEnum.DISCOUNT.getAttr(), ContractEnum.DISCOUNT.getAttrName());
        labelMap.put("self." + ContractEnum.OTHER_EXPENSE.getAttr(), ContractEnum.OTHER_EXPENSE.getAttrName());
        labelMap.put("self." + ContractEnum.DISCOUNT_AMOUNT.getAttr(), ContractEnum.DISCOUNT_AMOUNT.getAttrName());
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
    }
    public static void initTotalAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initFinishAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initUnFinishAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initBadAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initInvoiceAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initUnInvoiceAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initContractCost(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("合同成本= 合同产品总成本 - 退货产品成本 + 其他费用；其中“合同产品总成本”在实际出库前由预估产品成本计算而得，实际出库之后取实际产品成本计算而得。其中“退货产品成本”会在实际入库之后取实际产品成本计算而得。故而“合同成本”字段会根据实际情况进行变动及更新。(若历史数据不正确，请编辑合同订单并保存，系统会自动更正数据)");
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initGrossProfit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("合同毛利= 合同金额 - 退货金额 - 合同成本 (若历史数据不正确，请编辑合同订单并保存，系统会自动更正数据)");
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initGrossProfitRate(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.AVERAGE.getType());
        fieldAttrEntity.setMemo("合同毛利率 = 合同毛利/合同金额*100%");
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initCashProfit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("现金毛利 = 合同已收款金额 - 合同成本");
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initCashProfitRate(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.AVERAGE.getType());
        fieldAttrEntity.setMemo("现金毛利率 = 现金毛利 / 合同已收款金额");
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }
    public static void initArchived(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ArchivedEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initIsRed(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(IsRedEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initHasRed(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(HasRedEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initShowProduce(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ContractProduceEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initAllOutBound(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(AllOutBoundEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initIfStatistic(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(IfStatisticEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(1);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        //可删除权限：默认【超级管理员、销售主管】可删除负责人，支持自定义；
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setMultiple(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initProductTotal(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setMemo("默认值为产品销售价总计");
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setEditable(0);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + ContractEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + ContractEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.售价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
    }

    public static void initOtherExpense(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initDiscountAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initShipStatus(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsDictionary(1);
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(ShipStatusEnum.class, fieldAttrEntity.getFieldType());
        List<String> shipStatusCodeList = Arrays.asList(ShipStatusEnum.UNSHIPPED.getName(),ShipStatusEnum.DELIVERED.getName(),ShipStatusEnum.SHIPPED.getName(),ShipStatusEnum.CLOSED.getName());
        for (ItemPoJo itemPoJo: itemPoJos) {
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
            if (!shipStatusCodeList.contains(itemPoJo.getText())) {
                itemPoJo.setShowType(ShowTypeEnum.NONE.getCode());
                itemPoJo.setEditHide(1);
            } else {
                if (Objects.equals(itemPoJo.getText(), ShipStatusEnum.UNSHIPPED.getName())) {
                    itemPoJo.setChecked(true);
                }
            }
        }
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_OPTION_RELY.getAlias(), ForbiddenSettingEnum.DISABLE_SELF_DEFINE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setItems(itemPoJos);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
    }

    /**
     * 初始化应收账期
     */
    public static void initReceivablePeriod(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsOpen(BasicConstant.ZERO);
        fieldAttrEntity.setNoRepeat(BasicConstant.ZERO);
        fieldAttrEntity.setIsForbidden(BasicConstant.ZERO);
        fieldAttrEntity.setMemo(I18nMessageUtil.getMessage(I18nStringConstant.RECEIVABLE_PERIOD));
    }


    /**
     * 初始化已出库其他费用
     *
     * @param fieldAttrEntity 表单字段属性类
     */
    public static void initOutstockOtherExpense(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias()));
        fieldAttrEntity.setEditForbidden(BasicConstant.ZERO);
    }

    /**
     * 初始化已出库优惠金额
     *
     * @param fieldAttrEntity 表单字段属性类
     */
    public static void initOutstockDiscountAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias()));
        fieldAttrEntity.setEditForbidden(BasicConstant.ZERO);
    }
}
